package com.fast.common.userCache;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fast.base.web.SessionContext;
import com.fast.user.model.admin.AdminUser;
import com.fast.user.model.admin.Group;

public class UserRequestUtils {
	
	//系统运行日志操作文件
	private Log log = LogFactory.getLog(UserRequestUtils.class);
	
	/**
	 * 作用：得到当前登录者的权限信息<br/>
	 * @author 卢水发
	 * @return
	 * @since JDK 1.6
	 * String
	 */
	public static String getCurPmsArray() {
		 Group group = (Group) SessionContext.getAttr("group");
		 if(group!=null){
			 return group.getPermissionArray();
		 }else{
			 return null;
		 }
	}

	/**
	 * 绑定修改信息值
	 * @param member
	 * @param curAdminUser
	 * @return 
	 */
	public static  <T> void bindEditorInfo(T t) {
		try {
			AdminUser adminUser = AdminCacheUtils.getLoginAdminUser();
			if(null==adminUser) {
				return;
			}
			//获取该bean中的所有字段
			Field[] fields = t.getClass().getDeclaredFields();//获得属性
			//遍历map中的每一个值
			for (Field field : fields) {
				if(field.getName().toLowerCase().equals("editorid")){
					PropertyDescriptor pd = new PropertyDescriptor(field.getName(),t.getClass());
					Method setMethod = pd.getWriteMethod();
					setMethod.invoke(t,adminUser.getId());
				}else if(field.getName().toLowerCase().equals("editorname")){
					PropertyDescriptor pd = new PropertyDescriptor(field.getName(),t.getClass());
					Method setMethod = pd.getWriteMethod();
					setMethod.invoke(t,adminUser.getRealName());
				}else if(field.getName().toLowerCase().equals("edittime")){
					PropertyDescriptor pd = new PropertyDescriptor(field.getName(),t.getClass());
					Method setMethod = pd.getWriteMethod();
					setMethod.invoke(t,new Date());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 绑定创建信息
	 * @param member
	 * @param curAdminUser
	 * @return 
	 */
	public static <T> void bindCreaterInfo(T t) {
		try {
			AdminUser adminUser = AdminCacheUtils.getLoginAdminUser();
			if(null==adminUser) {
				return;
			}
			//获取该bean中的所有字段
			Field[] fields = t.getClass().getDeclaredFields();//获得属性
			//遍历map中的每一个值
			for (Field field : fields) {
				if(field.getName().toLowerCase().equals("createrid")){
					PropertyDescriptor pd = new PropertyDescriptor(field.getName(),t.getClass());
					Method setMethod = pd.getWriteMethod();
					setMethod.invoke(t,adminUser.getId());
				}else if(field.getName().toLowerCase().equals("creater")){
					PropertyDescriptor pd = new PropertyDescriptor(field.getName(),t.getClass());
					Method setMethod = pd.getWriteMethod();
					setMethod.invoke(t,adminUser.getRealName());
				}else if(field.getName().toLowerCase().equals("createtime")){
					PropertyDescriptor pd = new PropertyDescriptor(field.getName(),t.getClass());
					Method setMethod = pd.getWriteMethod();
					setMethod.invoke(t,new Date());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}